﻿import QtQuick 2.0

//测试用关卡
Item {
    id: itemLevel
    width: ballRadius*2*10 + 5*(10-1)
    anchors.horizontalCenter: parent.horizontalCenter
    y: 10

    property var arrays: new Array

    property var balls: new Array

    property real ballRadius: 50

    property int currentLevel: 1

    Component.onCompleted: {
        ready(1)
    }

    function ready(level){
        currentLevel = Math.min(level, 50)
        for(var k in balls){
            var kobj = balls[k]
            for(var ki=0 in kobj){
                kobj[ki].destroy()
            }
        }
        balls.length = 0

        arrays.length = 0

        //初始化
        for(var m=0 in _LEVELDATA[currentLevel-1]){
            var line = _LEVELDATA[currentLevel-1][m]
            var l = new Array
            for(var n=0 in line){
                l.push(line[n])
            }
            arrays.push(l)
        }

//        arrays = _LEVELDATA[currentLevel-1]

        for(var i in arrays){
            var line = arrays[i]
            var tempA = new Array
            var flag = i%2
            for(var j in line){
                var ball = Qt.createComponent("Ball.qml").createObject(itemLevel)

                if(flag==0){
                    //奇数
                    ball.x = ballRadius + j*(ballRadius*2+5)
                }
                else{
                    //偶数
                    ball.x = j*(ballRadius*2+5)
                }
                ball.y = i*ballRadius*2-7*i
                ball.type = line[j]

                tempA.push(ball)

            }
            balls.push(tempA)
        }
    }

    function addOne(i,j, type){
//        console.log("level.qml add ", i,j,type, balls.length)

        arrays[i][j] = type

        var obj = balls[i][j]
        obj.type = type

    }

    property var _LEVELDATA: [
        _LEVEL1,_LEVEL2,_LEVEL3,_LEVEL4,_LEVEL5,
    ]

    property var _LEVEL1: [
        [1,1,1,1,1,1,1,1,1],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],

        [0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
     ]
    property var _LEVEL2: [
        [1,0,0,0,0,0,0,0,0],
        [1,0,0,0,0,0,0,0,0,0],
        [1,0,0,0,0,0,0,0,0],
        [1,0,0,0,0,0,0,0,0,0],
        [1,0,0,0,0,0,0,0,0],
        [1,0,0,0,0,0,0,0,0,0],
        [1,0,0,0,0,0,0,0,0],
        [1,0,0,0,0,0,0,0,0,0],

        [0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
     ]
    property var _LEVEL3: [
        [1,1,1,1,1,1,1,1,1],
        [1,0,0,0,0,0,0,0,0,0],
        [1,0,0,0,0,0,0,0,0],
        [1,0,0,0,0,0,0,0,0,0],
        [1,0,0,0,0,0,0,0,0],
        [1,0,0,0,0,0,0,0,0,0],
        [1,0,0,0,0,0,0,0,0],
        [1,0,0,0,0,0,0,0,0,0],

        [0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
     ]
    property var _LEVEL4: [
        [1,1,1,1,1,1,1,1,1],
        [1,0,0,0,0,0,0,0,0,1],
        [1,0,0,0,0,0,0,0,1],
        [1,0,0,0,0,0,0,0,0,1],
        [1,0,0,0,0,0,0,0,1],
        [1,0,0,0,0,0,0,0,0,1],
        [1,0,0,0,0,0,0,0,1],
        [1,0,0,0,0,0,0,0,0,1],

        [0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
     ]
    property var _LEVEL5: [
        [1,1,1,1,1,1,1,1,1],
        [1,0,0,0,0,0,0,0,0,1],
        [1,0,0,0,0,0,0,0,1],
        [1,0,0,0,0,0,0,0,0,1],
        [1,0,0,0,0,0,0,0,1],
        [1,0,0,0,0,0,0,0,0,1],
        [1,0,0,0,0,0,0,0,1],
        [1,1,1,1,1,1,1,1,1,1],

        [0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
     ]
}

